package com.yulebaby.callsenter.adapter;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.orhanobut.logger.Logger;
import com.yulebaby.callsenter.R;
import com.yulebaby.callsenter.bean.SaleDailyBean;
import com.yulebaby.callsenter.utils.CommonUtil;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Zhangbo
 * Time: 2017/1/23
 */
public class DynamicDailyAdapter extends BaseQuickAdapter<SaleDailyBean, BaseViewHolder> {

    @BindView(R.id.tv_sign)
    TextView mTvSign;
    @BindView(R.id.tv_steptop)
    TextView mTvSteptop;
    @BindView(R.id.tv_stepbottom)
    TextView mTvStepbottom;
    @BindView(R.id.tv_trantarget)
    TextView mTvTrantarget;
    @BindView(R.id.tv_actualtran)
    TextView mTvActualtran;
    @BindView(R.id.et_unfinish_cause)
    EditText mEtUnfinishCause;
    @BindView(R.id.btn_cancel)
    Button mBtnCancel;
    @BindView(R.id.btn_save_cause)
    Button mBtnSaveCause;
    @BindView(R.id.tv_cause)
    TextView mTvCause;
    @BindView(R.id.btn_addcause)
    Button mBtnAddcause;
    @BindView(R.id.btn_addcause1)
    Button mBtnAddcause1;


    public DynamicDailyAdapter(List<SaleDailyBean> data) {
        super(R.layout.item_convers_target, data);
        Logger.d(data.size());
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, SaleDailyBean bean) {
        View convertView = baseViewHolder.getConvertView();
        convertView.setTag(bean.getStep());
        ButterKnife.bind(this, convertView);
        baseViewHolder.setText(R.id.tv_trantarget, "转化目标：" + bean.getTran_targetnumint())
                .setText(R.id.tv_actualtran, "实际转化：" + bean.getActual_tran())
                .setText(R.id.tv_cause, bean.getUnfinished_reason() != null ? bean.getUnfinished_reason() : "")
                .addOnClickListener(R.id.btn_cancel)
                .addOnClickListener(R.id.btn_addcause)
                .addOnClickListener(R.id.tv_actualtran)
                .addOnClickListener(R.id.btn_addcause1)
                .addOnClickListener(R.id.btn_save_cause);

        //1、为了避免TextWatcher在第2步被调用，提前将他移除。
        if (mEtUnfinishCause.getTag() instanceof TextWatcher) {
            mEtUnfinishCause.removeTextChangedListener((TextWatcher) (mEtUnfinishCause.getTag()));
        }

        if (bean.getCompletion_status() == 0) {
            mTvActualtran.setTextColor(CommonUtil.getColor(R.color.red));
            mTvSign.setTextColor(CommonUtil.getColor(R.color.red));
            mTvSign.setText("未完成");
        } else {
            mTvActualtran.setTextColor(CommonUtil.getColor(R.color.yulebaby_mask_color));
            mTvSign.setTextColor(CommonUtil.getColor(R.color.blue));
            mTvSign.setText("已完成");
        }

        switch (bean.getStep()) {
            case 2:
                mTvSteptop.setText("步骤1: 确认客户意向");
                mTvStepbottom.setText("步骤2: 客户明确表述需求");
                break;
            case 3:
                mTvSteptop.setText("步骤2: 客户明确表述需求");
                mTvStepbottom.setText("步骤3: 已阅览资料或参观店面");
                break;
            case 4:
                mTvSteptop.setText("步骤3: 已阅览资料或参观店面");
                mTvStepbottom.setText("步骤4: 明确知晓项目优势");
                break;
            case 5:
                mTvSteptop.setText("步骤4: 明确知晓项目优势");
                mTvStepbottom.setText("步骤5: 客户到访");
                break;
            case 6:
                mTvSteptop.setText("步骤5: 客户到访");
                mTvStepbottom.setText("步骤6: 已签约商铺");
                break;
            case 7:
                mTvSteptop.setText("步骤6: 已签约商铺");
                mTvStepbottom.setText("步骤7: 签合同并打款到账");
                break;
        }

        switch (bean.getItem_state()) {
            case 1:
                convertView.findViewById(R.id.ll_dynamic_edit).setVisibility(View.GONE);
                convertView.findViewById(R.id.layout_unfinished_cause).setVisibility(View.GONE);
                convertView.findViewById(R.id.layout_unfinished_cause1).setVisibility(View.GONE);
                convertView.findViewById(R.id.btn_addcause).setVisibility(View.GONE);
                break;
            case 2:
                convertView.findViewById(R.id.ll_dynamic_edit).setVisibility(View.VISIBLE);
                convertView.findViewById(R.id.layout_unfinished_cause).setVisibility(View.GONE);
                convertView.findViewById(R.id.layout_unfinished_cause1).setVisibility(View.GONE);
                convertView.findViewById(R.id.btn_addcause).setVisibility(View.VISIBLE);
                break;
            case 3:
                convertView.findViewById(R.id.ll_dynamic_edit).setVisibility(View.VISIBLE);
                convertView.findViewById(R.id.layout_unfinished_cause).setVisibility(View.GONE);
                convertView.findViewById(R.id.layout_unfinished_cause1).setVisibility(View.VISIBLE);
                convertView.findViewById(R.id.btn_addcause).setVisibility(View.GONE);
                break;
            case 4:
                convertView.findViewById(R.id.ll_dynamic_edit).setVisibility(View.VISIBLE);
                convertView.findViewById(R.id.layout_unfinished_cause).setVisibility(View.VISIBLE);
                convertView.findViewById(R.id.layout_unfinished_cause1).setVisibility(View.GONE);
                convertView.findViewById(R.id.btn_addcause).setVisibility(View.GONE);
                break;
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0)
                    bean.setUnfinished_reason(s.toString().trim());
            }
        };
        mEtUnfinishCause.addTextChangedListener(watcher);
        mEtUnfinishCause.setTag(watcher);
    }

}
